<template>
  <div>
    <el-dialog :title="form.id?'编辑客户':'新增客户'" :visible="isShow" @close="closeFn">
      <el-form ref="form" :model="form" label-position="top" :rules="rules">
        <el-form-item label="经纪" prop="agent">
          <el-select v-model="form.agent" placeholder="请选择经纪人">
            <el-option
              v-for="item in agentList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="微信" prop="wechat">
          <el-input v-model="form.wechat" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="form.desc" type="textarea" />
        </el-form-item>
        <el-form-item label="封面">
          <UploadImage ref="img1" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-row type="flex" justify="center">
          <el-button size="small" @click.native="closeFn">取消</el-button>
          <el-button size="small" type="primary" @click.native="submit">确定</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { addClients, getClientsById, updateClientsById } from '@/api/clients/index'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    agentList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        image: '',
        email: '',
        phone: '',
        agent: '',
        wechat: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空！', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号不能为空！', trigger: 'blur' },
          { pattern: /^1[2-9]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        wechat: [
          { required: true, message: '微信号不能为空！', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空！', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式不正确', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空！', trigger: 'blur' }
        ],
        agent: [
          { required: true, message: '描述不能为空！', trigger: 'blur' }
        ]
      }

    }
  },
  async created() {
    // await this.formId ? this.form = this.formId : {}

  },
  methods: {
    // 清除图片
    removeImg() {
      this.imgUrl = ''
    },
    async getClientsById(id) {
      const res = await getClientsById(id)
      this.form = res.data
    },
    // 关闭
    closeFn() {
      this.$emit('update:isShow', false)
      this.form = {
        name: '',
        image: '',
        email: '',
        phone: '',
        agent: '',
        wechat: '',
        desc: '',
        id: ''
      }
    },
    // 提交
    async submit() {
      await this.$refs.form.validate()
      if (this.form.id) {
        this.form.image = this.$refs.img1.fileList[0].url
        await updateClientsById(this.form)
      } else {
        this.form.image = this.$refs.img1.imgUrl
        await addClients(this.form)
      }
      this.closeFn()
      this.$emit('updateEvent')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
